<template>
  <div>
    <div id="search">
      <el-form :inline="true" :model="TemplateForm" class="demo-form-inline">
        <el-form-item label="课程名称">
          <el-input v-model="TemplateForm.fileName" placeholder="请输入模版名字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"  icon="el-icon-search" @click="onSearchSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div id="header-bth">
      <el-button type="primary" size="small" icon=el-icon-circle-plus-outline @click="uploadtemplate">上传模版</el-button>
    </div>
    <div id="role-table">
      <el-table
          :header-cell-style="{background:'rgb(242, 242, 242)'}"
          style="width: 100%;
          text-align: center"
          :data="tableData"
          :row-style="{height: '50px'}"
          :cell-style="{padding: '0'}"
      >
        <el-table-column label="模版名字">
          <template slot-scope="scope">
            <span>{{ scope.row.fileName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="存储名字" width="450">
          <template slot-scope="scope">
            <span>{{ scope.row.uuidName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="存储大小">
          <template slot-scope="scope">
            <span>{{ scope.row.size}}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" >
          <template slot-scope="scope">
            <span>{{ scope.row.createTime}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div id="nav-page">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="pageSizes"
          :current-page="TemplateForm.page"
          :page-size="TemplateForm.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!--批量导入文件弹窗    -->
    <el-dialog title="文件上传" :visible.sync="uploadDialogVisible" width="27%" @close="clearfile">
      <el-upload
          ref='upload'
          drag
          class="upload-demo"
          :headers="config"
          action='#'
          name='file'
          accept=".xls,.xlsx"
          :limit="1"
          :show-file-list="false"
          :http-request="uploadFile"
          :on-success="uploadSuccess"
          :on-error="uploadError"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import instance from "@/request/request";
import {uploadtemplate} from "@/request/api";

export default {
  data() {
    return {
      tableData: [],
      pageSizes: [3, 5, 10, 15],
      total: 0,
      TemplateForm: {  // 搜索数据
        page: 1,
        limit: 5
      },
      uploadDialogVisible:false,
      config:{
        "token":sessionStorage.getItem("token")
      }
    }
  },
  created() {
    this.gettemplateList()
  },
  methods: {
    //上传模版信息
    uploadFile(file){
      // 创建表单数据对象 封装file数据
      let formData = new FormData();
      // username = tom    file.file 获取上传文件数据
      formData.append("file",file.file)
      uploadtemplate(formData).then(res=>{
       if(res.code==200){
         this.$message.success(res.msg)
         this.uploadDialogVisible=false
         this.gettemplateList()
         this.$refs.upload.clearFiles();
         return
       }
      }).catch(erro=>{
        this.$message.error("连接失败")
      })
    },
    //获取模版信息
    gettemplateList() {
      instance.get("/template/getlist",{params:this.TemplateForm}).then(res=>{
        if(res.code==200){
          this.tableData=res.data.template
          this.total=res.data.total
          return
        }
        this.$message.error(res.msg)
      }).catch(erro=>{
        this.$message.error("请求失败！")
      })
    },
    //批量导入成功
    uploadSuccess(){
      this.$message.error("导入成功")
      this.uploadDialogVisible=false
      this.gettemplateList()
    },
    // 批量导入失败
    uploadError(){
      this.$message.error("上传失败")
      this.uploadDialogVisible=false
    },
    // 删除模版
    handleDelete(row) {
      this.$confirm('确认要删除？', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const name=row.uuidName
        instance.delete("/template/delete/"+row.id,
            {params: {uuidname: name}}).then(res=>{
          if(res.code==200){
            this.$message.success(res.msg)
            this.gettemplateList()
            return
          }
          this.$message.warning(res.msg)
          this.gettemplateList()
        }).catch(erro=>{
          this.$message.error("请求失败！")
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消！'
        })
      })
    },
    // 搜索角色
    onSearchSubmit() {
      this.TemplateForm.page = 1
      this.TemplateForm.limit = 5
      this.gettemplateList()
    },
    // 改变每页显示的条数触发
    handleSizeChange(val) {
      this.TemplateForm.limit = val;
      this.gettemplateList()
    },
    // 改变页码触发
    handleCurrentChange(val) {
      this.TemplateForm.page = val;
      this.gettemplateList()
    },
    // 显示弹出等
    uploadtemplate() {
       this.uploadDialogVisible=true
    },
    clearfile(){
      this.$refs.upload.clearFiles()
    }
  },
}
</script>

<style scoped>
#search {
  width: 100%;
  height: 50px;
  border-bottom: 1px solid #eee;
}

#search, #header-bth, #nav-page {
  margin: 15px;
}

#role-table {
  width: 100%;
}
</style>
